<?php
namespace Controllers;


class loginController extends Controller
{   
    public function __construct(\Slim\Container $container)
    {
        $this->container = $container;
        date_default_timezone_set('Asia/Shanghai');
        // 允许所有域访问
        header("Access-Control-Allow-Origin: http://localhost:8080");
        header("Access-Control-Allow-Credentials:true");
        header("Access-Control-Allow-Methods:PUT, GET, POST, DELETE, OPTIONS");
        header("Access-Control-Allow-Headers:Content-Type");   
        ini_set("session.cookie_httponly", 1);
    }

    public function checkLogin($request, $response, $args) 
    {
        // 检查是否post了数据
        if(!isset($_POST['username']) && !isset($_POST['password']))
        {
            return self::jsonFail('please input the username or password!');
        }
        else
        {
            // 验证
            $res = $this->container['db']->select($this->userTable, '*', [
                'uid' => $_POST['username'],
                'password' => $_POST['password'],
                'status[!]' => 0,
                'is_delete[!]' => 1
            ]);
            if(!$res)
            {
                return self::jsonFail('username or password error!');
            }
            else
            {
                $ip = $request->getAttribute('ip_address');
                $login_time = date("Y-m-d h:i:sa", time());
                // 填写payload
                $payload = array();
                $payload['iss'] = 'gongxun';
                $payload['iat'] = $_SERVER['REQUEST_TIME'];
                $payload['exp'] = $_SERVER['REQUEST_TIME'] + 7200;
                //工号
                $payload['uid'] = $res[0]['uid'];
                $payload['username'] = $res[0]['username'];
                $payload['id'] = $res[0]['id'];
                $data['uid'] = $res[0]['uid'];
                $data['username'] = $res[0]['username'];
                $data['last_login_time'] = $res[0]['login_time'];
                $data['last_login_ip'] = $res[0]['login_ip'];
                $data['phone'] = $res[0]['telnum'];
                $ip = $_SERVER['REMOTE_ADDR'];
                $login_time = date("Y-m-d h:i:sa", time());
                $this->container['db']->update($this->userTable, [
                    'login_time' => $login_time,
                    'login_ip' => $ip
                ], [
                    'id' => $res[0]['id']
                ]);
                // 获取角色
                $role = $this->container['db']->select($this->roleUserTable, 'role_id', [
                    'user_id' => $res[0]['id']
                ]);
                if(!$role) 
                {
                    return self::jsonFail('this account doesn\'t have a role');
                }
                else
                {
                    // 定义死五种权限 多于十种权限则需要采用别的方法
                     $data['position'] = '';
                     $payload['position'] = '';
                    foreach ($role as $key => $role_id) 
                    {
                        $data['position'] .= $role_id;
                        $payload['position'] .= $role_id;
                    }
                }
                $data['token'] = $this->encode($payload, $this->key);
                @session_start();
                $_SESSION['token'] = $data['token'];
                return self::jsonSuccess('login success', $data);
            }
        }
    }

    public function checkToken($request, $response, $args) 
    {
        @session_start();
        //在访问前先检查token是否合法
        // echo $_SESSION['token'];
        if(!isset($_SESSION['token']))
        {
            echo "false";
        }
        else
        {
            $token = $_SESSION['token'];
            $payload = self::decode($token, $this->key);
            if(!$payload)
            {
                return "false";
            }
            else
            {
                return "true";
            }
        }
    }
}